package runInfo;
//import java.sql.ResultSet;
import java.sql.SQLException;

//import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
//import org.eclipse.swt.custom.CCombo;

//import AnalysisSQLjet.SQLanalysis;
//import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;

//import sql_h2.databaseMngGUI;

//import pill_tracker_sql_h2.Database_Query_Interface;


import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import com.phidgets.*;
import com.phidgets.event.*;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.graphics.Font;
//import org.eclipse.jface.resource.JFaceResources;

//import runInfo.NewThreadExample.MessageLoop;

//import WhiskeyNewThread.

//import runInfo.Temperature;

//import Temperature;


public class mainGui {

    public static ArrayList<Double> tempTCArr 	= new ArrayList<Double>();  //  @jve:decl-index=0:
    public static ArrayList<Double> tempTFArr 	= new ArrayList<Double>();  //  @jve:decl-index=0:
    public static ArrayList<Double> tempACArr 	= new ArrayList<Double>();  //  @jve:decl-index=0:
    public static ArrayList<Double> tempAFArr 	= new ArrayList<Double>();
    
    public static ArrayList<String> dateTime 	= new ArrayList<String>();  //  @jve:decl-index=0:
    public static ArrayList<Double> timeS	 	= new ArrayList<Double>();  //  @jve:decl-index=0:
    public static ArrayList<Integer> bottle 	= new ArrayList<Integer>();  //  @jve:decl-index=0:
    public static ArrayList<Double> boilerTemp 	= new ArrayList<Double>();  //  @jve:decl-index=0:
    public static ArrayList<Double> abv 		= new ArrayList<Double>();  //  @jve:decl-index=0:

    public static String testingS = "testies";
    
    public static int bottling = 0;
    
	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="97,44"
	private Button Startbutton = null;
	private Button Stopbutton = null;
	boolean headerFlag = true;
	boolean writeOut = false;

	TemperatureSensorPhidget tempsensor;
	
	public Label tcOut = null;

	public static double tempC = 0.0;
	public static double tempF = 0.0;
	public static double tempAC = 0.0;
	public static double tempAF = 0.0;
	
	
	
	public static long t0;
	public Text tempTextC = null;
	private Text temptextC1 = null;
	private Text temptextF1 = null;
	static Text tempTextF = null;
	private Text tctext1 = null;
	private Text coltext = null;
	private Text coltextC = null;
	private Text coltextF = null;
	private Text temptextA = null;
	public Text temptextAC = null;
	private Text temptextAF = null;
	private Text text1 = null;
	private Text text11 = null;
	private Browser browser = null;
	private Browser browser1 = null;
	private Text text2 = null;
	private ProgressBar progressBar = null;
	private Text measBrixText = null;
	private Text coltext1 = null;
	private Text coltext11 = null;
	private Text measBrixTempText = null;
	private Text coltext111 = null;
	private Text coltext1111 = null;
	private Text coltext2 = null;
	private Text measAbvText = null;
	private Text coltext112 = null;
	private Text coltext1112 = null;
	private Text corrAbvText = null;
	private Text coltext11121 = null;
	private Text text111 = null;
	private Text boilerTempText = null;
	private Combo bottleCombo = null;
	private Text text1121 = null;
	private Text corrBrixText = null;
	private Text potenAbvText = null;
	private Text coltext11111 = null;
	private Combo distTempCombo = null;
	
	private mainGui(){
//		this.tempTCArr = new ArrayList<Double>();
//		this.temptextC = new Text;
	
	}
	
	
	private void WriteData() throws IOException {		//Write out Files ================================================================	
		String Dir = "C:\\WhiskeyData\\output.txt";
		// CheckForHeaderExistance & if beginning time is less than 12 hours...?
		if(headerFlag == true){
				FileWriter write = new FileWriter(Dir, true);
				PrintWriter text = new PrintWriter(write, true);
				String output = "datetime"  + "\t" + "min" + "\t" + "columnC" + "\t" + "columnF" + "\t" + "ambientC" + "\t" + "ambientF" 
											+ "\t" + "bottleNumber" + "\t" + "correctedAbv" + "\t" + "uncorrectedAbv" + "\t" + "distillateTempC"
											+ "\t" + "boilerTempC";
				System.out.println(output);
				text.println(output);
				text.flush();
				write.close();
				text.close();
				write = null;
				text = null;
				headerFlag = false;
		}
			try{	
				FileWriter write = new FileWriter(Dir, true);
				PrintWriter text = new PrintWriter(write, true);

		        DecimalFormat df1 = new DecimalFormat("###.##");
		        DecimalFormat df2 = new DecimalFormat("#####.#####");
				Double time = (Double.valueOf((System.currentTimeMillis()))-t0)/1000.0/60.0;

				System.out.println(String.valueOf(tempTCArr.get(tempTCArr.size()-1)));
				
				String output = CurrentTime()  	+ "\t" + df2.format(time) + "\t" + String.valueOf(tempTCArr.get(tempTCArr.size()-1)) 
												+ "\t" + String.valueOf(tempTFArr.get(tempTCArr.size()-1))  	+ "\t" + String.valueOf(tempACArr.get(tempTCArr.size()-1))  
												+ "\t" + String.valueOf(tempTFArr.get(tempTCArr.size()-1))  	+ "\t" + String.valueOf(bottle.get(bottle.size()-1)) 
//												+ "\t" + corrAbvText.getText() + "\t" + measAbvText.getText() 	+ "\t" + boilerTempText.getText();
												+ "\t" + distTempCombo.getText() + "\t" + Data.corrAbvS + "\t" + Data.measAbvS + "\t" + Data.boilTempS ;

				System.out.println(output);
				text.println(output);
				text.flush();
				write.close();
				text.close();
				write = null;
				text = null;				
			}
			catch (IOException ie){		}
	}
		
	private void createBrowser() {
		browser = new Browser(sShell, SWT.NONE);
		browser.setUrl("C:\\WhiskeyData\\Complete.jpeg");
	}

	private void createBrowser1() {
		GridData gridData = new GridData();
		browser1 = new Browser(sShell, SWT.NONE);
		browser1.setUrl("C:\\WhiskeyData\\Segment.jpeg");
		browser1.setLayoutData(gridData);
	}

	private void createBottleCombo() {
		bottleCombo = new Combo(sShell, SWT.NONE);
		String[] bottleItems = {"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"};
		bottleCombo.setItems(bottleItems );
		bottleCombo.setTextLimit(30);
		bottleCombo.setText("00");
		bottleCombo.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		bottleCombo.setVisibleItemCount(10);
		bottleCombo.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
			public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
				bottling = bottleCombo.getSelectionIndex();
			}
		});
		bottleCombo
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
					    Data.bottleS = bottleCombo.getItem(bottleCombo.getSelectionIndex());
					}
					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
	}
	
	/**
	 * This method initializes DistTempcombo	
	 *
	 */
	private void createDistTempcombo() {
		distTempCombo = new Combo(sShell, SWT.NONE);
		distTempCombo.setFont(new Font(Display.getDefault(), "Segoe UI Light", 16, SWT.BOLD));
		distTempCombo.setText("01");
		String[] items = {"1 C", "2 C", "3 C", "4 C", "5 C", "6 C", "7 C", "8 C", "9 C", "10 C", "11 C", "12 C", "13 C", "14 C", "15 C", "16 C", "17 C", "18 C", "19 C", "20 C", "21 C", "22 C", "23 C", "24 C", "25 C", "26 C", "27 C", "28 C", "29 C", "30 C", "31 C", "32 C", "33 C", "34 C", "35 C", "36 C", "37 C", "38 C", "39 C", "40 C", "41 C", "42 C", "43 C", "44 C", "45 C", "46 C", "47 C", "48 C", "49 C", "50 C"};

		distTempCombo.setItems(items );
		distTempCombo
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						System.out.println("widgetSelected()"); // TODO Auto-generated Event stub widgetSelected()
						
					}
					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
	
	}

	public static void main(String[] args) throws PhidgetException, IOException, InterruptedException {
		Display display = Display.getDefault();
		mainGui thisClass = new mainGui();
		thisClass.createSShell();
		thisClass.sShell.open();
		t0 = System.currentTimeMillis();
		
		while (!thisClass.sShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();	
	}
	
	public static String CurrentTime(){
	    Calendar calendar = new GregorianCalendar();
	    String am_pm;
	    int year = calendar.get(Calendar.YEAR);
	    int month = calendar.get(Calendar.MONTH);
	    int day = calendar.get(Calendar.DAY_OF_MONTH); 
	    int hour = calendar.get(Calendar.HOUR);
	    int minute = calendar.get(Calendar.MINUTE);
	    int second = calendar.get(Calendar.SECOND);
	    if(calendar.get(Calendar.AM_PM) == 0)
	    	am_pm = "AM";
	    else{
	    	hour = hour + 12;
	      	am_pm = "PM";
	    }
//      	String times = year + "-" + month  + "-" +day + "\t" + hour + ":"+ minute + ":" + second + " " + am_pm;
//      	String times = year + "-" + month  + "-" +day + " " + hour + ":"+ minute + ":" + second;
      	Data.dateTimeS = year + "-" + month  + "-" +day + " " + hour + ":"+ minute + ":" + second;
	    
      	return Data.dateTimeS;
	  }
//
	public void createSShell() throws PhidgetException, InterruptedException {
		GridData gridData2 = new GridData();
		gridData2.horizontalSpan = 13;
		GridData gridData1 = new GridData();
		gridData1.horizontalSpan = 13;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 17;
		sShell = new Shell();
		sShell.setText("Whiskey Run");
//		sShell.setImage(new Image(Display.getCurrent(), "C:/WhiskeyData/Complete.bmp"));
		sShell.setFont(new Font(Display.getDefault(), "Segoe UI", 18, SWT.NORMAL));
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(1527, 465));
		//---------
		Label filler1 = new Label(sShell, SWT.NONE);
		Label filler52 = new Label(sShell, SWT.NONE);
		Label filler48 = new Label(sShell, SWT.NONE);
		Label filler281 = new Label(sShell, SWT.NONE);
		coltext1 = new Text(sShell, SWT.NONE);
		measBrixText = new Text(sShell, SWT.BORDER);
		measBrixText.setText("000.000");
		measBrixText.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		measBrixText.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
			public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
			    //TO DO:
				// calculate corrected Brix value

			}
		});
		coltext11121 = new Text(sShell, SWT.NONE);
		coltext11121.setEditable(false);
		coltext11121.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext11121.setText("Bottle #");
		createBottleCombo();
		Label filler13 = new Label(sShell, SWT.NONE);
		Label filler511 = new Label(sShell, SWT.NONE);
		Label filler512 = new Label(sShell, SWT.NONE);
		Label filler38 = new Label(sShell, SWT.NONE);
		coltext = new Text(sShell, SWT.NONE);
		coltext.setEditable(false);
		coltext.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext.setText("Sensor");
		coltextC = new Text(sShell, SWT.CENTER);
		coltextC.setEditable(false);
		coltextC.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltextC.setText("C");
		coltextF = new Text(sShell, SWT.CENTER);
		coltextF.setEditable(false);
		coltextF.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltextF.setText("F");
		//---------
		Label filler34 = new Label(sShell, SWT.NONE);
		Label filler45 = new Label(sShell, SWT.NONE);
		Label filler2 = new Label(sShell, SWT.NONE);
		Label filler27 = new Label(sShell, SWT.NONE);
		Label filler97 = new Label(sShell, SWT.NONE);
		coltext1.setEditable(false);
		coltext1.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext1.setText("Measured Brix");
		coltext11111 = new Text(sShell, SWT.NONE);
		coltext11111.setEditable(false);
		coltext11111.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext11111.setText("Ph Temp adjustment");
		coltext11 = new Text(sShell, SWT.NONE);
		coltext11.setEditable(false);
		coltext11.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext11.setText("Measured Temp");
		measBrixTempText = new Text(sShell, SWT.BORDER);
		measBrixTempText.setText("29.0000");
		measBrixTempText.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		measBrixTempText.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
			public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
				//To Do:
				// calculate modified Brix value
				
			}
		});
		text111 = new Text(sShell, SWT.NONE);
		boilerTempText = new Text(sShell, SWT.BORDER);
		boilerTempText.setText("000.000");
		boilerTempText.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		Label filler12 = new Label(sShell, SWT.NONE);
		boilerTempText.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
			public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
			    Data.boilTempS = boilerTempText.getText();
			}
		});
		Label filler47 = new Label(sShell, SWT.NONE);
		Label filler42 = new Label(sShell, SWT.NONE);
		text1121 = new Text(sShell, SWT.NONE);
		text1121.setEditable(false);
		text1121.setText("     ");
		temptextA = new Text(sShell, SWT.NONE);
		temptextA.setEditable(false);
		temptextA.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		temptextA.setText("Board: ");
		temptextAC = new Text(sShell, SWT.NONE);
		temptextAC.setEditable(false);
		temptextAC.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		temptextAC.setText("000.000");
		temptextAF = new Text(sShell, SWT.NONE);
		temptextAF.setEditable(false);
		temptextAF.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		temptextAF.setText("000.000");
		//---------
		Label filler33 = new Label(sShell, SWT.NONE);
		Label filler44 = new Label(sShell, SWT.NONE);
		Label filler3 = new Label(sShell, SWT.NONE);
		Label filler25 = new Label(sShell, SWT.NONE);
		Label filler20 = new Label(sShell, SWT.NONE);
		Label filler32 = new Label(sShell, SWT.NONE);
		coltext111 = new Text(sShell, SWT.NONE);
		coltext111.setEditable(false);
		coltext111.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext111.setText("Corrected Brix");
		corrBrixText = new Text(sShell, SWT.NONE);
		corrBrixText.setEditable(false);
		corrBrixText.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		corrBrixText.setText("000.00");
		coltext2 = new Text(sShell, SWT.NONE);
		coltext2.setEditable(false);
		coltext2.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext2.setText("Measured abv");
		measAbvText = new Text(sShell, SWT.BORDER);
		measAbvText.setText("000.000");
		measAbvText.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		measAbvText.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
			public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
				if(!(measAbvText.getText().isEmpty()) && !(distTempCombo.getText().isEmpty())){
			    	TempCorrections TempCor = new TempCorrections();
					corrAbvText.setText(String.valueOf(TempCor.GetABV(Double.valueOf(measAbvText.getText()), distTempCombo.getText())));
				}
			}
		});
		text111.setEditable(false);
		text111.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		text111.setText("boiler temp");
		Label filler5 = new Label(sShell, SWT.NONE);
		Label filler59 = new Label(sShell, SWT.NONE);
		Label filler5101 = new Label(sShell, SWT.NONE);
		Label filler37 = new Label(sShell, SWT.NONE);
		temptextC1 = new Text(sShell, SWT.WRAP);
		temptextC1.setEditable(false);
		temptextC1.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		temptextC1.setText("Thermocouple:    ");
		tempTextC = new Text(sShell, SWT.NONE);
		tempTextC.setEditable(false);
		tempTextC.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		tempTextC.setText("000.000");
		tempTextF = new Text(sShell, SWT.NONE);
		tempTextF.setEditable(false);
		tempTextF.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		tempTextF.setText("000.000");
		//---------		
		Label filler31 = new Label(sShell, SWT.NONE);
		Label filler43 = new Label(sShell, SWT.NONE);
		Label filler8 = new Label(sShell, SWT.NONE);
		Label filler24 = new Label(sShell, SWT.NONE);
		Label filler93 = new Label(sShell, SWT.NONE);
		Label filler510 = new Label(sShell, SWT.NONE);
		coltext1111 = new Text(sShell, SWT.NONE);
		coltext1111.setEditable(false);
		coltext1111.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext1111.setText("Potential abv");
		potenAbvText = new Text(sShell, SWT.NONE);
		potenAbvText.setEditable(false);
		potenAbvText.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		potenAbvText.setText("000.00");
		coltext112 = new Text(sShell, SWT.NONE);
		coltext112.setEditable(false);
		coltext112.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext112.setText("Distillate Temp");
		distTempCombo = new Combo(sShell, SWT.NONE);
		distTempCombo.setFont(new Font(Display.getDefault(), "Segoe UI Light", 16, SWT.BOLD));
		distTempCombo.setText("29 C");
		String[] items = {"1 C", "2 C", "3 C", "4 C", "5 C", "6 C", "7 C", "8 C", "9 C", "10 C", "11 C", "12 C", "13 C", "14 C", "15 C", "16 C", "17 C", "18 C", "19 C", "20 C", "21 C", "22 C", "23 C", "24 C", "25 C", "26 C", "27 C", "28 C", "29 C", "30 C", "31 C", "32 C", "33 C", "34 C", "35 C", "36 C", "37 C", "38 C", "39 C", "40 C", "41 C", "42 C", "43 C", "44 C", "45 C", "46 C", "47 C", "48 C", "49 C", "50 C"};
		distTempCombo.setItems(items);
		distTempCombo
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						if(!(measAbvText.getText().isEmpty()) && !(distTempCombo.getText().isEmpty())){
					    	TempCorrections TempCor = new TempCorrections();
							corrAbvText.setText(String.valueOf(TempCor.GetABV(Double.valueOf(measAbvText.getText()), distTempCombo.getText())));
						}
					}
					public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {}
				});

		Label filler21 = new Label(sShell, SWT.NONE);
		Label filler17 = new Label(sShell, SWT.NONE);
		Label filler16 = new Label(sShell, SWT.NONE);
		Label filler36 = new Label(sShell, SWT.NONE);
		Startbutton = new Button(sShell, SWT.NONE);
		Startbutton.setText("Start Recording");
		Startbutton.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		tctext1 = new Text(sShell, SWT.NONE);
		tctext1.setEditable(false);
		tctext1.setText("             ");
		Label filler39 = new Label(sShell, SWT.NONE);
		Label filler15 = new Label(sShell, SWT.NONE);
		Label filler30 = new Label(sShell, SWT.NONE);
		Label filler10 = new Label(sShell, SWT.NONE);
		text11 = new Text(sShell, SWT.NONE);
		text11.setEditable(false);
		text11.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		text11.setText("Plot - Beginning");
		text1 = new Text(sShell, SWT.NONE);
		text1.setEditable(false);
		text1.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		text1.setText("Plot - Segment      ");
		Label filler4 = new Label(sShell, SWT.NONE);
		Label filler11 = new Label(sShell, SWT.NONE);
		Label filler14 = new Label(sShell, SWT.NONE);
		coltext1112 = new Text(sShell, SWT.NONE);
		coltext1112.setEditable(false);
		coltext1112.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		coltext1112.setText("Corrected abv");
		corrAbvText = new Text(sShell, SWT.NONE);
		corrAbvText.setEditable(false);
		corrAbvText.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		corrAbvText.setText("000.00");
		Label filler = new Label(sShell, SWT.NONE);
		Label filler18 = new Label(sShell, SWT.NONE);
		Label filler19 = new Label(sShell, SWT.NONE);
		Label filler35 = new Label(sShell, SWT.NONE);
		Stopbutton = new Button(sShell, SWT.NONE);
		Stopbutton.setText("Stop Recording");
		Stopbutton.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		Label filler46 = new Label(sShell, SWT.NONE);
		Label filler6 = new Label(sShell, SWT.NONE);
		Label filler29 = new Label(sShell, SWT.NONE);
		Label filler41 = new Label(sShell, SWT.NONE);
		Label filler7 = new Label(sShell, SWT.NONE);
		createBrowser();
		createBrowser1();
		text2 = new Text(sShell, SWT.BORDER);
		text2.setText("information updating..............");
		text2.setFont(new Font(Display.getDefault(), "Segoe UI", 16, SWT.NORMAL));
		text2.setLayoutData(gridData1);
		Label filler40 = new Label(sShell, SWT.NONE);
		Label filler9 = new Label(sShell, SWT.NONE);
		Label filler22 = new Label(sShell, SWT.NONE);
		progressBar = new ProgressBar(sShell, SWT.NONE);
		progressBar.setLayoutData(gridData2);
		
		Startbutton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
					writeOut = true;
			}
		});
		Stopbutton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
					writeOut = false;
			}
		});

		final Thread t = new Thread(new MeasurementLoop());
		t.start();
		sShell.addShellListener(new org.eclipse.swt.events.ShellAdapter() {
			public void shellClosed(org.eclipse.swt.events.ShellEvent e) {
				System.out.println("closed shell::::::::::::::");
				try {
					t.join();
//					t.stop();
//					t.interrupt();
//					t.join();
					t.destroy();
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
//				t.interrupt();
//				t.join();				//shouldn't be long now - wait indefinitely
			}
		});

		t.interrupt();
//		sShell.dispose();
	}

	private class MeasurementLoop implements Runnable{
		boolean stopping = false;
		Thread currentThread = null;

		
//		@Override
		public void run(){
			
		    currentThread = Thread.currentThread();
		    
			long maxRunTime = 1000;						// ms
			maxRunTime = 1000*maxRunTime;				// s
			long startTime = System.currentTimeMillis();
			long currentTime = System.currentTimeMillis();			
			TemperatureSensorPhidget tempsensor;
			try {
				tempsensor = new TemperatureSensorPhidget();
				tempsensor.openAny();
				System.out.println("waiting for new thread TemperatureSensor attachment...");
				tempsensor.waitForAttachment();
				tempsensor.setTemperatureChangeTrigger(0, 0.2);
				tempsensor.setThermocoupleType(0, 4);
				while(Data.programRunning){
//					while (!isStopping()) {
//				while((currentTime-startTime<=maxRunTime)){
					currentTime = System.currentTimeMillis();
				    Data.timeS 	= String.valueOf(((currentTime-startTime)/1000.0));
				    GetTemps(tempsensor, startTime);
					System.out.println("temp = " + Data.tempTC + "measuring temp for: " + Data.timeS + " s");
					System.out.println("program running? " + Data.programRunning); 
					if(writeOut)WriteData();
					try{
						Thread.sleep((long) 1000);			// sleep for X ms
					}catch(InterruptedException e){
					}
				}			
			} catch (PhidgetException e2) {
				e2.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("No longer measureing temperature ------------------------------------");
		}
		


		public void GetTemps(TemperatureSensorPhidget tempsensor, Long startTime ) throws PhidgetException, IOException{
			double calibration = 0.3;
			Data.tempTC = tempsensor.getTemperature(0)-calibration;
			Data.tempAC = tempsensor.getAmbientTemperature()-calibration;
			Data.tempTF = Data.tempTC*9/5 + 32;
			Data.tempAF = Data.tempAC*9/5 + 32;	//			System.out.println("finished acquiring temps");

			DecimalFormat df = new DecimalFormat("###.###");
			Data.tempTC = Double.valueOf(df.format(Data.tempTC));
			Data.tempAC = Double.valueOf(df.format(Data.tempAC));
			Data.tempTF = Double.valueOf(df.format(Data.tempTF));
			Data.tempAF = Double.valueOf(df.format(Data.tempAF));
		

			 System.out.println("----------HERE-----------");
			 System.out.println("----------HERE-----------");
			 System.out.println("----------HERE-----------");
			 System.out.println("----------HERE-----------");
			 System.out.println("----------HERE-----------");
//			 if(!(sShell.isEnabled())) Data.programRunning = false;
//			 System.out.println("program running? " + Data.programRunning);
//			 System.out.println("disposed? " + 	Display.getCurrent().isDisposed());
			 
			 
			if(writeOut){
				mainGui.tempTCArr.add(Data.tempTC);
				mainGui.tempTFArr.add(Data.tempTF);
				mainGui.tempACArr.add(Data.tempAC);
				mainGui.tempAFArr.add(Data.tempAF);

				double runningTime = System.currentTimeMillis()-startTime;				
				Data.timeS = String.valueOf(runningTime); 	
				mainGui.dateTime.add(CurrentTime());
				mainGui.timeS.add(runningTime);
				mainGui.bottle.add(bottling);
				mainGui.boilerTemp.add(Data.tempTC);
				mainGui.abv.add(Data.tempTC);	
				WriteData();
				
				Display.getCurrent().isDisposed();
			}
			Display.getDefault().asyncExec(new Runnable() {
				 public void run() {
					 tempTextC.setText(String.valueOf(Data.tempTC));
					 tempTextF.setText(String.valueOf(Data.tempTF));
					 temptextAC.setText(String.valueOf(Data.tempAC));
					 temptextAF.setText(String.valueOf(Data.tempAF));
//					 temptextAF.TestText("000.000");
					 Data.corrAbvS = corrAbvText.getText();
					 Data.measAbvS = measAbvText.getText();
					 Data.boilTempS = boilerTempText.getText();
					 System.out.println("is enabled? " + sShell.isEnabled());
//					 if(!(sShell.isEnabled())) Data.programRunning = false;
//					 System.out.println("program running? " + Data.programRunning);
					 
//					 if((sShell.isDisposed())) Data.programRunning = false;
//					 if((sShell.isListening(0))) Data.programRunning = false;
//					 if(!(sShell.isVisible())) Data.programRunning = false;
				}
			});
		}
		

		
	}
}
